package com.asa.control_theory.utils;

import com.asa.control_theory.A;
import com.asa.control_theory.utils.bean.ZhuangTai_ZhuanYi_Matrix;
import com.asa.hutils.MathHUtils;


/**
 * 	传递函数的使用
 *	通过传递函数，计算状态矩阵
 * 	这个方法没找到实例测过的，不报错，但不确定asa这个方法是否可行
 */
public class GongShi5 {
	
	
	
	public static void main(String[] args) {
		System.out.println("===================yingyong====================");
		
		double[][] a1 = {{0,1},
				{0,-2}};
		
//		create_zhuangtaizhuanyi_matrix_eAt(a1);
		
		
		double[] x0 = {0,0};
		double[][] asa_x0 = {x0};
		asa_x0 = MathHUtils.zhuanzhi(asa_x0);
		double[][] B = {{0},{1}};
		
		matrix2value(a1,1,0,asa_x0,B);

		
		
		
	}

	/**
	 * 	通过状态矩阵来
	 * @param a1
	 * @param t
	 * @param t0
	 * @param x0
	 * @return
	 */
	private static double[][] matrix2value(double[][] a1,double t,double t0,double[][] x0,double[][] B) {
		ZhuangTai_ZhuanYi_Matrix hamilton_eAt_creatematrix = GongShi4.Hamilton_eAt_creatematrix(a1);
		
		return zhuangtaizhuanyi_matrix2value(x0,B, hamilton_eAt_creatematrix,t,t0);
	}

	/**
	 * 通过状态转移矩阵来计算状态值
	 * 
	 * 拉氏变换的乘积等于卷积，公式中的B部分的积分变换是卷积
	 * @param x0
	 * @param hamilton_eAt_creatematrix
	 * @param t
	 * @param t0
	 * @return xt
	 */
	public static double[][] zhuangtaizhuanyi_matrix2value(double[][] x0,double[][] B,
			ZhuangTai_ZhuanYi_Matrix hamilton_eAt_creatematrix,double t,double t0) {
		double[][] move_t = hamilton_eAt_creatematrix.move_t(1);
//		double[][] dao = hamilton_eAt_creatematrix.dao(1);
//		double[][] dingjifen = hamilton_eAt_creatematrix.dingjifen(1, 0);
		
		
		double[][] chenfa = MathHUtils.chenfa(move_t, x0);
		MathHUtils.print(chenfa);
		double[][] asa = asa(hamilton_eAt_creatematrix, t, t0);
		
		
		System.out.println("-------------");
		double[][] chenfa2 = MathHUtils.chenfa(asa, B);
		MathHUtils.print(chenfa2);
		return chenfa2;
	}

	/**
	 * 通过状态转移矩阵来计算输出值
	 * 拉氏变换的乘积等于卷积，公式中的B部分的积分变换是卷积
	 * @param x0
	 * @param B
	 * @param hamilton_eAt_creatematrix
	 * @param t
	 * @param t0
	 * @return
	 */
	public static double[][] zhuangtaizhuanyi_matrix2value_y(double[][] x0,double[][] u,double[][] B,double[][] C,double[][] D,
			ZhuangTai_ZhuanYi_Matrix hamilton_eAt_creatematrix,double t,double t0) {
		double[][] zhuangtaizhuanyi_matrix2value = zhuangtaizhuanyi_matrix2value(x0, B, hamilton_eAt_creatematrix, t, t0);
		
		double[][] chenfa = MathHUtils.chenfa(C, zhuangtaizhuanyi_matrix2value);
		
		double[][] chenfa2 = MathHUtils.chenfa(D, u);
		double[][] result = MathHUtils.jiafa(chenfa, chenfa2);
		
		return result;
		
	}
	
	
	
	
	/**
	 * 积分的计算
	 * 卷积计算
	 * @param hamilton_eAt_creatematrix
	 * @param t
	 * @param t0
	 * @return
	 */
	public static double[][] asa(ZhuangTai_ZhuanYi_Matrix hamilton_eAt_creatematrix,double t,double t0) {
		
		double buchang = 0.001;
		
		double tao = t0;
		
		
		double[][] move_t = hamilton_eAt_creatematrix.move_t(tao);
		
		double[][] result = new double[move_t.length][move_t.length];
		
		boolean isbigger= false;
		while (true) {
			if (tao>=t) {
				tao=t;
				isbigger = true;
			}else {
				tao += buchang;
			}
			
			double x = t-tao;
			
			double[][] now_move_t = hamilton_eAt_creatematrix.move_t(x);
			
			double[][] jiafa = MathHUtils.jiafa(move_t, now_move_t);
			
			double[][] chenfa = MathHUtils.chenfa(jiafa, 0.5*buchang);
			

			result = MathHUtils.jiafa(result, chenfa);
			
			move_t = now_move_t;
			
			if (isbigger) {
				break;

			}
		}
		
		return result;
		
		
	}
	
	
	/**
	 * 状态传函数
	 * eAt的值用近似解来做
	 */
	public static double[][] asa(double[][] A_matrix,double t,int k){
		
		return A.eAt(A_matrix, t, k);
	}
	
	
	
	
	/**
	 * 	通过状态矩阵来，使用的是迭代的方法
	 * @param a1
	 * @param t
	 * @param t0
	 * @param x0
	 * @return
	 */
	public static double[][] matrix2value_diedai(double[][] a1,double t,double t0,double[][] x0,double[][] B) {

		ZhuangTai_ZhuanYi_Matrix tai_ZhuanYi_Matrix = new ZhuangTai_ZhuanYi_Matrix();
		
		
		double[][] eAt = asa(a1, t, 100);
		double[][] chenfa = MathHUtils.chenfa(eAt, x0);
		
		
		//头大，后面那个卷积要怎么求呀
		double buchang = 0.001;
		
		double tao = t0;
		
				
		double[][] result = new double[a1.length][a1.length];
		double[][] move_t = eAt;

		
		
		
		boolean isbigger= false;
		while (true) {
			if (tao>=t) {
				tao=t;
				isbigger = true;
			}else {
				tao += buchang;

			}
		
			
			
			double[][] asa = asa(a1, t-tao, 100);
			double[][] chenfa2 = MathHUtils.chenfa(asa, B);
			double[][] now_move_t  = MathHUtils.chenfa(chenfa2, B);

			double[][] move_t2 = tai_ZhuanYi_Matrix.move_t(tao);
			
			now_move_t = MathHUtils.chenfa(now_move_t, move_t2);
			
			double[][] jiafa = MathHUtils.jiafa(move_t, now_move_t);

			double[][] dd = MathHUtils.chenfa(jiafa, 0.5*buchang);
			
			result = MathHUtils.jiafa(result, dd);//积分，梯形计算公式，上底加下底，乘高除以2

			
			move_t = now_move_t;
			
			if (isbigger) {
				break;

			}
		
		}
		
		return MathHUtils.jiafa(chenfa, result);
	}
	
	
	
	
	
	
	
	
	
	
	
}
